home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 / Aminet - June 1993 [Walnut Creek].iso / usenet / sources / volume90 / examples / serial next >
Encoding:
Text File  |  1990-03-04  |  6.0 KB  |  224 lines

  1. Path: xanth!cs.odu.edu!Amiga-Request
  2. From: Amiga-Request@cs.odu.edu (Amiga Sources/Binaries Moderator)
  3. Newsgroups: comp.sources.amiga
  4. Subject: v90i091: serial - example of use of serial.device, Part01/01
  5. Message-ID: <11615@xanth.cs.odu.edu>
  6. Date: 4 Mar 90 00:36:28 GMT
  7. Sender: tadguy@cs.odu.edu
  8. Reply-To: Simon Raybould <S.J.Raybould@fulcrum.bt.co.uk>
  9. Lines: 210
  10. Approved: tadguy@cs.odu.edu (Tad Guy)
  11. X-Mail-Submissions-To: Amiga@cs.odu.edu
  12. X-Post-Discussions-To: comp.sys.amiga
  13.  
  14. Submitted-by: Simon Raybould <S.J.Raybould@fulcrum.bt.co.uk>
  15. Posting-number: Volume 90, Issue 091
  16. Archive-name: examples/serial
  17.  
  18. I have seen a few requests for info about the serial.device so here is an
  19. example program to show how to set the baud rate, parity e.t.c. and then
  20. issue reads and writes
  21.  
  22. Please don't flame me about the code it is just an example as to how to set
  23. up the serial device and issue writes and reads. I think all of the type
  24. casts are correct but this really isn't the issue here.
  25.  
  26. For more information you should see the Rom Kernel Reference manual includes
  27. and autodocs and Libs and devices.
  28.  
  29. I havn't been able to get hold of a copy of Libs&Devs in this country yet so
  30. all the info is from includes and autodocs. But as soon as I see a copy I
  31. shall buy it.
  32.  
  33. Cheers,
  34.       Simon Raybould
  35.  
  36. #!/bin/sh
  37. # This is a shell archive.  Remove anything before this line, then unpack
  38. # it by saving it into a file and typing "sh file".  To overwrite existing
  39. # files, type "sh file -c".  You can also feed this as standard input via
  40. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  41. # will see the following message at the end:
  42. #        "End of archive 1 (of 1)."
  43. # Contents:  serial.c
  44. # Wrapped by tadguy@xanth on Sat Mar  3 19:36:05 1990
  45. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  46. if test -f 'serial.c' -a "${1}" != "-c" ; then 
  47.   echo shar: Will not clobber existing file \"'serial.c'\"
  48. else
  49. echo shar: Extracting \"'serial.c'\" \(3318 characters\)
  50. sed "s/^X//" >'serial.c' <<'END_OF_FILE'
  51. X/*
  52. X *
  53. X *  Description : Example of how to set up and use the serial device.
  54. X *                This simple example reads chars from the serial device
  55. X *                and echoes them back to the serial device. It also maps
  56. X *                CR to NL + CR just for a bit of fun. It is not intended
  57. X *                to do anything wizzo, just outline the basic steps needed
  58. X *                to setup and use the serial.device on the Amiga.
  59. X *
  60. X *  Author      : Simon Raybould     (sie@fulcrum.bt.co.uk)
  61. X *
  62. X *  Date        : 14th Feb 1990
  63. X *
  64. X */
  65. X
  66. X
  67. X#include <exec/types.h>
  68. X#include <devices/serial.h>
  69. X#include <libraries/dos.h>
  70. X
  71. Xstruct MsgPort    *CreatePort();
  72. Xstruct IORequest    *CreateExtIO();
  73. Xvoid DeletePort();
  74. Xvoid DeleteExtIO();
  75. X
  76. Xstruct MsgPort    *serialport;
  77. Xstruct IOExtSer    *serialIO;
  78. Xshort openerror;
  79. X
  80. Xmain()
  81. X{
  82. X    void write(), cleanexit(), SetUpSerial();
  83. X    char c=0, read();
  84. X
  85. X    serialport = CreatePort(0L, 0L);
  86. X    if(!serialport)
  87. X        cleanexit(RETURN_FAIL);
  88. X
  89. X
  90. X    serialIO=(struct IOExtSer *)
  91. X        CreateExtIO(serialport,(long)sizeof(struct IOExtSer));
  92. X    if(!serialIO)
  93. X        cleanexit(RETURN_FAIL+1);
  94. X    if(openerror=OpenDevice("serial.device",0L,serialIO,0L))
  95. X        cleanexit(RETURN_FAIL+2);
  96. X    
  97. X
  98. X    serialIO->IOSer.io_Message.mn_ReplyPort = serialport;
  99. X
  100. X    SetUpSerial();    /* Set baud rate, parity, stop bits  e.t.c. */
  101. X
  102. X    while(c != 'q' && c != 'Q') {
  103. X        c = read();
  104. X        if(c == '\r')
  105. X            write('\n');
  106. X        write(c);
  107. X    }
  108. X
  109. X    cleanexit(RETURN_OK);
  110. X}
  111. X
  112. X/*
  113. X * Function: read() - blocks and aits for the next char on the serial
  114. X *                    devide. It then returns the next char received.
  115. X *
  116. X * Author: Simon Raybould.
  117. X *
  118. X * Date  : 14th Feb 1990.
  119. X */
  120. Xchar read()
  121. X{
  122. X    char c=0;
  123. X
  124. X    serialIO->IOSer.io_Command = CMD_READ;
  125. X    serialIO->IOSer.io_Length = 1;
  126. X    serialIO->IOSer.io_Data = (APTR)&c;
  127. X    
  128. X    DoIO(serialIO);
  129. X    return c;
  130. X}
  131. X
  132. X/*
  133. X * Function: write() - writes the char passed to the seial device.
  134. X *
  135. X * Author: Simon Raybould.
  136. X *
  137. X * Date  : 14th Feb 1990.
  138. X */
  139. X
  140. Xvoid write(c)
  141. Xchar c;
  142. X{
  143. X    serialIO->IOSer.io_Command = CMD_WRITE;
  144. X    serialIO->IOSer.io_Length = 1;
  145. X    serialIO->IOSer.io_Data = (APTR)&c;
  146. X    
  147. X    DoIO(serialIO);
  148. X}
  149. X
  150. X/*
  151. X * Function: SetUpSerial() - Set BAUD, PARITY, STOP BITS e.t.c.
  152. X *                           this is hard coded in this example just to 
  153. X *                           show where these values should go.
  154. X *
  155. X * Author: Simon Raybould.
  156. X *
  157. X * Date  : 14th Feb 1990.
  158. X */
  159. X
  160. Xvoid SetUpSerial()
  161. X{
  162. X    serialIO->IOSer.io_Command = SDCMD_SETPARAMS;
  163. X    serialIO->io_Baud = 1200;    /* Set this to the baud rate */
  164. X    /*
  165. X     * Set mode to odd parity 
  166. X     * remove the SERF_PARTY_ODD for EVEN parity
  167. X     * remove the SERF_PARTY_ON for no parity i.e set to 0 for none
  168. X     */
  169. X    serialIO->io_SerFlags = SERF_PARTY_ON | SERF_PARTY_ODD;
  170. X    serialIO->io_ExtFlags = 0;    /* If not used must be set to 0 !!!! */
  171. X    serialIO->io_StopBits = 1;
  172. X
  173. X    DoIO(serialIO);
  174. X}
  175. X
  176. X/*
  177. X * Function: Cleanexit() - Graceful exit routine.
  178. X *
  179. X * Author: Taken from RKM 1.3 Includes and Autodocs (SECTION B First page).
  180. X *         And yes it realy does say that reading legal mush can turn your
  181. X *         brain to gucamole!!!
  182. X *
  183. X */
  184. X
  185. Xvoid cleanexit(returncode)
  186. Xint returncode;
  187. X{
  188. X    if(returncode!=RETURN_OK)
  189. X        printf("Failed to open device\n");
  190. X
  191. X    if(!openerror)    CloseDevice(serialIO);
  192. X    if(serialIO)    DeleteExtIO(serialIO,(long)sizeof(struct IOExtSer));
  193. X    if(serialport)    DeletePort(serialport);
  194. X
  195. X    exit(returncode);
  196. X}
  197. END_OF_FILE
  198. if test 3318 -ne `wc -c <'serial.c'`; then
  199.     echo shar: \"'serial.c'\" unpacked with wrong size!
  200. fi
  201. # end of 'serial.c'
  202. fi
  203. echo shar: End of archive 1 \(of 1\).
  204. cp /dev/null ark1isdone
  205. MISSING=""
  206. for I in 1 ; do
  207.     if test ! -f ark${I}isdone ; then
  208.     MISSING="${MISSING} ${I}"
  209.     fi
  210. done
  211. if test "${MISSING}" = "" ; then
  212.     echo You have the archive.
  213.     rm -f ark[1-9]isdone
  214. else
  215.     echo You still need to unpack the following archives:
  216.     echo "        " ${MISSING}
  217. fi
  218. ##  End of shell archive.
  219. exit 0
  220. -- 
  221. Mail submissions (sources or binaries) to <amiga@cs.odu.edu>.
  222. Mail comments to the moderator at <amiga-request@cs.odu.edu>.
  223. Post requests for sources, and general discussion to comp.sys.amiga.
  224.